package javafx.officialdemo.scroll;

import java.io.File;
import java.io.RandomAccessFile;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class FXScrollpane extends Application{
	private static final String bookname = "d.txt";

	private TextArea text = null;
	
	private Button next = null;
	private Button pre = null;
	private Button ok = null;
	private TextField progress = null;
	ScrollPane sp = new ScrollPane();
	
	public void start(Stage stage) throws Exception {
		
		Group root = new Group();
		text = new TextArea();
		text.setEditable(false);
		text.setPrefSize(600, 380);
		text.setWrapText(true);
		text.setStyle("-fx-background-color: #3ec74e");
		
		sp.setContent(text);
		sp.autosize();
		sp.setTranslateX(10);
		sp.setTranslateY(10);
		sp.setStyle("-fx-background-color: #3ec74e");
		root.getChildren().add(sp);
		
		HBox hBox = new HBox();
		
		next = new Button("next");
		pre = new Button("back");
		progress = new TextField();
		progress.setMaxWidth(50);
		ok = new Button("ok");
		Label label = new Label("Go:");
		label.setAlignment(Pos.BASELINE_CENTER);
		
		hBox.getChildren().addAll(label,progress,ok,new Label("	"),pre,next);
		hBox.setSpacing(10);
		hBox.setTranslateX(10);
		hBox.setTranslateY(405);//
		root.getChildren().add(hBox);
		
		Scene scene = new Scene(root);
		stage.setScene(scene);
		stage.show();
		
		LoadBook();
		addAction();
	}
	
	String bookPath = "D:\\"+bookname;
	long  interval = 1024;
	long schedule = 0;
	long bookLen = -1;

	double dSchedule = 0;
	String strSchedule = "0";

	String code = "gb2312";
	RandomAccessFile raf = null;
	File book = new File(bookPath);
	byte[] bytes = new byte[1024];
	
	public void LoadBook(){
		Runnable run = new Runnable() {
			public void run() {
				//初始化
				if(raf == null){
					try {
						raf = new RandomAccessFile(book,"r");
						bookLen = raf.length();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				//加载
				try {
					raf.seek(schedule);
					int read = raf.read(bytes);
					final String txt = new String(bytes,code);
					schedule = raf.getFilePointer();
					
					dSchedule = ((double)schedule/(double)bookLen)*100;
					strSchedule = dSchedule+"";
					Platform.runLater(new Runnable() {
						public void run() {
							text.setText(txt);
							sp.setVvalue(0);
							String string = strSchedule.substring(0, strSchedule.indexOf(".")+3);
							progress.setText(string);//75.94
							
							int len = txt.length();
//							for(int i=0;i<len;i++){
//								System.out.print(txt.charAt(i));
//								if(i%20 ==0 ){
//									System.out.println();
//								}
//							}
						}
					});
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		};
		
		new Thread(run).start();
	}
	
	public void addAction(){
		next.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				LoadBook();
			}
		});
		
		pre.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				schedule = schedule -interval*2;
				LoadBook();
			}
		});
		
		ok.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				
				String strJump = progress.getText();
				try {
					
					double jumpTo = Double.parseDouble(strJump);
					schedule = (long) (bookLen * jumpTo/100);
					LoadBook();
					
				} catch (Exception e) {
					System.out.println("请输入合法数字...");
				}
			}
		});
	}
	
	private String getCodeType(byte[] head) {  
	    byte[] codehead = new byte[4];  
	    // 截取数组  
	    System.arraycopy(head, 0, codehead, 0, 4);  
	    String code = "";  
	    if(head[0] == -1 && head[1] == -2) {  
	        code = "UTF-16";  
	    }  
	    else if(head[0] == -2 && head[1] == -1) {  
	        code = "Unicode";  
	    }  
	    else if(head[0] == -17 && head[1] == -69 && head[2] == -65)  
	        code = "UTF-8";  
	    else {  
	        code = "gb2312";  
	    }  
	    return code;  
	}  
	
	public static void main(String[] args) {
		
		launch(null);
		
	}
}
